package com.cgnpc.scp.manager.controller;

import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.utils.AjaxResultUtil;
import com.cgnpc.scp.manager.mybatis.entity.ParameterConfig;
import com.cgnpc.scp.manager.service.IParameterConfigService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Locale;

/******************************
 * 用途说明: 参数配置
 * 作者姓名: yblrs
 * 创建时间: 2020/8/16 17:55
 ******************************/
@Controller
@RequestMapping("/scp/data/parameterConfig")
public class ParameterConfigController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ParameterConfigController.class);

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private CurrentUser currentUser;

    @Autowired
    private IParameterConfigService parameterConfigService;

    /**********************************
     * 用途说明: 参数配置---分页查询
     * 参数说明: HttpServletRequest request, @RequestBody T reqEntity
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "参数配置---分页查询")
    @UBA(module = "系统管理", action = "参数配置分页查询", channel = OperatorType.Page)
    @RequestMapping(value = "/loadByPaging", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends ParameterConfig> Object loadByPaging(HttpServletRequest request, @RequestBody T reqEntity) {
        AjaxResult ajaxResult = null;

        try {
            MyPageResult<T> myPageResult = parameterConfigService.loadByPaging(reqEntity);
            ajaxResult = AjaxResultUtil.createSuccessMessage(myPageResult);
        } catch (Exception e) {
            LOGGER.error("loadByPaging.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

    /**********************************
     * 用途说明: 参数配置---保存或修改
     * 参数说明: HttpServletRequest request, @RequestBody List<T> reqEntityList
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "参数配置---保存或修改")
    @UBA(module = "系统管理", action = "保存参数配置", channel = OperatorType.Button)
    @RequestMapping(value = "/saveOrUpdate", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends ParameterConfig> Object saveOrUpdate(HttpServletRequest request, @RequestBody List<T> reqEntityList) {
        AjaxResult ajaxResult = null;

        try {
            // 获取当前的登录用户信息
            UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);
            ajaxResult = parameterConfigService.saveOrUpdate(loginUserInfo, reqLocale, reqEntityList);
        } catch (Exception e) {
            LOGGER.error("saveOrUpdate.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

    /**********************************
     * 用途说明: 参数配置---删除
     * 参数说明: HttpServletRequest request, @RequestBody ParameterConfig reqParameterConfig
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "参数配置---删除")
    @UBA(module = "系统管理", action = "删除参数配置", channel = OperatorType.Button)
    @RequestMapping(value = "/del", method = {RequestMethod.POST})
    @ResponseBody
    public Object del(HttpServletRequest request, @RequestBody ParameterConfig reqParameterConfig) {
        AjaxResult ajaxResult = null;

        try {
            // 获取当前的登录用户信息
            UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);
            ajaxResult = parameterConfigService.del(loginUserInfo, reqLocale, reqParameterConfig);
        } catch (Exception e) {
            LOGGER.error("del.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

    /**********************************
     * 用途说明: 参数配置---查询指定的参数配置
     * 参数说明: HttpServletRequest request, @RequestBody ParameterConfig reqParameterConfig
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "参数配置---查询指定的参数配置")
    @UBA(module = "系统管理", action = "查看参数配置详情", channel = OperatorType.Button)
    @RequestMapping(value = "/queryByParamKey", method = {RequestMethod.POST})
    @ResponseBody
    public Object queryByParamKey(HttpServletRequest request, @RequestBody ParameterConfig reqParameterConfig) {
        AjaxResult ajaxResult = null;

        try {
            // 获取当前的登录用户信息
            // UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            // Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);
            List<ParameterConfig> dbParameterConfigList = parameterConfigService.queryByParamKey(reqParameterConfig);
            ajaxResult = AjaxResultUtil.createSuccessMessage(dbParameterConfigList);
        } catch (Exception e) {
            LOGGER.error("del.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

}
